<template>
  <div id="notice">
    <div>通知管理</div>
    <div id="table">
      <el-button size="mini" type="primary" @click="myExport"> 打印 </el-button>
      <el-button size="mini" type="primary" @click="myExport"> 导出 </el-button>
      <el-button size="mini" type="primary" @click="dialogFormVisible = true">
        添加
      </el-button>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="标题" prop="title" sortable> </el-table-column>
        <el-table-column label="内容" prop="content" sortable>
        </el-table-column>
        <el-table-column label="日期" prop="time" sortable> </el-table-column>
        <el-table-column label="发送人" prop="sender" sortable>
        </el-table-column>
        <el-table-column align="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="primary"
              @click="handleDelete(scope.$index, scope.row)"
            >
              查看
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 弹出框表单 -->
    <el-dialog title="通知" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="标题" :label-width="formLabelWidth">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="内容" :label-width="formLabelWidth">
          <el-input v-model="form.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="日期" :label-width="formLabelWidth">
          <el-input
            v-model="form.time"
            autocomplete="off"
            type="datetime-local"
          ></el-input>
        </el-form-item>
        <el-form-item label="发送人" :label-width="formLabelWidth">
          <el-input v-model="form.sender" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addNotice">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {
        title: "",
        content: "",
        time: "",
        sender: "",
      },
      formLabelWidth: "60px",
    };
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    myExport() {
      console.log("打印");
    },
    // 添加通知
    addNotice() {
      console.log("添加通知");
      let that = this;
      this.dialogFormVisible = false;
      axios
        .post(
          "http://localhost:8080/notice/addNotice",
          this.$qs.stringify(that.form)
        )
        .then(function (response) {
          if (response.data) {
            that.$message({
              message: "添加成功",
              type: "success",
            });
            that.getNoticeDate();
            that.form.title = ""
            that.form.content = ""
            that.form.time = ""
            that.form.sender = ""
          } else {
            that.$message({
              message: "添加失败",
              type: "error",
            });
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    // 获取通知信息
    getNoticeDate() {
      let that = this;
      axios
        .get("http://localhost:8080/notice/getAll")
        .then(function (response) {
          that.tableData = response.data;
        })
        .catch(function (error) {
          console.log(error);
        });
    },
  },
  // 初始化表格
  created() {
    this.getNoticeDate();
  },
};
</script>

<style scoped>
#table {
  margin: 20px;
  width: 80%;
}
</style>